<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>优化docker打包速度和体积 | 叫我华仔~</title>
    <meta name="description" content="Write the Code. Change the world">
    <link rel="shortcut icon" type="image/x-icon" href="https://tva1.sinaimg.cn/large/0060lm7Tly1g4lrkxbno3j30go0gogm9.jpg">
    
    <link rel="preload" href="/assets/css/0.styles.cd3776eb.css" as="style"><link rel="preload" href="/assets/js/app.48b22e4d.js" as="script"><link rel="preload" href="/assets/js/2.3fdd559a.js" as="script"><link rel="preload" href="/assets/js/9.2299158f.js" as="script"><link rel="prefetch" href="/assets/js/10.e1dd09fa.js"><link rel="prefetch" href="/assets/js/11.4aed7e88.js"><link rel="prefetch" href="/assets/js/12.82f25f15.js"><link rel="prefetch" href="/assets/js/13.0fee9ac6.js"><link rel="prefetch" href="/assets/js/14.12b766db.js"><link rel="prefetch" href="/assets/js/15.867ab787.js"><link rel="prefetch" href="/assets/js/3.378e0b39.js"><link rel="prefetch" href="/assets/js/4.3e162dee.js"><link rel="prefetch" href="/assets/js/5.3b714238.js"><link rel="prefetch" href="/assets/js/6.80a92dc6.js"><link rel="prefetch" href="/assets/js/7.f7efdc2b.js"><link rel="prefetch" href="/assets/js/8.a13d01b5.js">
    <link rel="stylesheet" href="/assets/css/0.styles.cd3776eb.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">叫我华仔~</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/nodejs/JavaScript/JavaScript面试题大全.html" class="nav-link">Nodejs</a></div><div class="nav-item"><a href="/database/mysql/Explain详解.html" class="nav-link">Database</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Linux-Deploy</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/linux-deploy/docker/优化docker打包速度和体积.html" class="nav-link">Docker</a></li></ul></div></div><div class="nav-item"><a href="/tools/Blog推荐.html" class="nav-link">Tools</a></div><div class="nav-item"><a href="/user/me.html" class="nav-link">关于我</a></div><div class="nav-item"><a href="https://github.com/zwhgithub" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/nodejs/JavaScript/JavaScript面试题大全.html" class="nav-link">Nodejs</a></div><div class="nav-item"><a href="/database/mysql/Explain详解.html" class="nav-link">Database</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">Linux-Deploy</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/" class="nav-link">Linux</a></li><li class="dropdown-item"><!----> <a href="/linux-deploy/docker/优化docker打包速度和体积.html" class="nav-link">Docker</a></li></ul></div></div><div class="nav-item"><a href="/tools/Blog推荐.html" class="nav-link">Tools</a></div><div class="nav-item"><a href="/user/me.html" class="nav-link">关于我</a></div><div class="nav-item"><a href="https://github.com/zwhgithub" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Github
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Docker</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/linux-deploy/docker/优化docker打包速度和体积.html" class="active sidebar-link">优化docker打包速度和体积</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h3 id="优化docker打包速度，减少镜像体积。"><a href="#优化docker打包速度，减少镜像体积。" aria-hidden="true" class="header-anchor">#</a> 优化docker打包速度，减少镜像体积。</h3> <div class="warning custom-block"><p>Docker 的层用于保存镜像的上一版本和当前版本之间的差异。就像 Git 的提交一样，如果你与其他存储库或镜像共享它们，就会很方便。</p></div> <h3 id="docker镜像是有很多层"><a href="#docker镜像是有很多层" aria-hidden="true" class="header-anchor">#</a> docker镜像是有很多层</h3> <div class="tip custom-block"><p>大家在定义 Dockerfile 时，如果太多的使用 RUN 指令，经常会导致镜像有特别多的层，镜像很臃肿，而且甚至会碰到超出最大层数（127层）限制的问题，遵循 Dockerfile 最佳实践，我们应该把多个命令串联合并为一个 RUN（通过运算符&amp;&amp;和/ 来实现），每一个 RUN 要精心设计，确保安装构建最后进行清理，这样才可以降低镜像体积，以及最大化的利用构建缓存。</p></div> <p><strong>当前并不是只使用一条RUN的指令就好</strong></p> <p><strong>构建业务服务镜像技巧</strong></p> <blockquote><p>Docker 在 build 镜像的时候，如果某个命令相关的内容没有变化，会使用上一次缓存（cache）的文件层，在构建业务镜像的时候可以注意下面三点。</p></blockquote> <ul><li>不变或者变化很少的体积较大的依赖库和经常修改的自有代码分开。</li> <li>因为 cache 缓存在运行 Docker build 命令的本地机器上，建议固定使用某台机器来进行 Docker build，以便利用 cache。</li> <li>用好缓存和基础镜像, 经常变更的层放后面。</li></ul> <h3 id="总结下来dockerfile-构建技巧"><a href="#总结下来dockerfile-构建技巧" aria-hidden="true" class="header-anchor">#</a> 总结下来dockerfile 构建技巧</h3> <div class="tip custom-block"><ul><li>多阶段构建、如npm i 单独构建。</li> <li>npm 的时候 npm install —production  (部分项目全部node_modules都下载下来了)。</li> <li>.dockerignore忽略一些生产环境的代码执行没有帮助的。(如：test .gitlab-ci.yml 等等，我试验过，大约可以减少5M体积。)</li></ul></div> <h3 id="来个简单的示例"><a href="#来个简单的示例" aria-hidden="true" class="header-anchor">#</a> 来个简单的示例</h3> <div class="language-dockerfile line-numbers-mode"><pre class="language-text"><code>FROM node:10-alpine

WORKDIR /usr/src/app

RUN apk --update add tzdata \
    &amp;&amp; cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    &amp;&amp; echo &quot;Asia/Shanghai&quot; &gt; /etc/timezone \
    &amp;&amp; apk del tzdata \
    &amp;&amp; mkdir -p /usr/src/app

# add npm package
COPY package.json /usr/src/app/package.json

RUN npm i --production --registry=https://registry.npm.taobao.org

# copy code
COPY . /usr/src/app

EXPOSE 7001

CMD npm start
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
    <script src="/assets/js/app.48b22e4d.js" defer></script><script src="/assets/js/2.3fdd559a.js" defer></script><script src="/assets/js/9.2299158f.js" defer></script>
  </body>
</html>
